//
//  xsSelectBossTemplateView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
//  选择老板IP模板
// @[@"我的",@"男性",@"女性"]

#import "xsSelectBossTemplateView.h"
//#import "Masonry.h"
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsSelectBossTemplateView()<UIScrollViewDelegate,xsSelectDigitalRobotCollectionViewProtoCol>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xButton;// x按钮


@property (nonatomic, strong) UIScrollView *buttonsBgScrollView;
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) UIView *buttonsBgView;

@property (nonatomic,strong)UIScrollView *myManWomenBgScrollView;// 我的 男性  女性

@property (nonatomic,strong)xsSelectDigitalRobotCollectionView *meCollectionView;// 我的
@property (nonatomic,strong)xsSelectDigitalRobotCollectionView *manCollectionView;//  男性
@property (nonatomic,strong)xsSelectDigitalRobotCollectionView *womenCollectionView;// 女性

@end

@implementation xsSelectBossTemplateView



- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
        
        [self addSubview:self.topView];
        
        [self.topView addSubview:self.searchButton];
        [self.topView addSubview:self.topTitleLabel];
        [self.topView addSubview:self.xButton];
        
        [self addSubview:self.buttonsBgScrollView];
        [self.buttonsBgScrollView addSubview:self.buttonsBgView];
        
        
        [self addSubview:self.myManWomenBgScrollView];
        [self.myManWomenBgScrollView addSubview:self.meCollectionView];
        [self.myManWomenBgScrollView addSubview:self.manCollectionView];
        [self.myManWomenBgScrollView addSubview:self.womenCollectionView];
        
        // 创建按钮数组
        self.buttons = buttonTitles;//@[@"Tab1", @"Tab2", @"Tab3"];
        
    }
    return self;
}


- (UIView *)buttonsBgView{
    if(!_buttonsBgView){
        _buttonsBgView = [[UIView alloc] init];
        _buttonsBgView.backgroundColor = UIColor.blackColor;
    }
    return _buttonsBgView;
}


- (UIScrollView *)buttonsBgScrollView
{
    if(!_buttonsBgScrollView){
        _buttonsBgScrollView = [[UIScrollView alloc] init];
        _buttonsBgScrollView.backgroundColor = UIColor.clearColor;
        
        _buttonsBgScrollView.delegate = self;
        //_buttonsBgScrollView.pagingEnabled = YES;
        _buttonsBgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _buttonsBgScrollView;
}

- (UIScrollView *)myManWomenBgScrollView
{
    if(!_myManWomenBgScrollView){
        _myManWomenBgScrollView = [[UIScrollView alloc] init];
        _myManWomenBgScrollView.backgroundColor = UIColor.clearColor;
        
        _myManWomenBgScrollView.delegate = self;
        _myManWomenBgScrollView.pagingEnabled = YES;
        _myManWomenBgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _myManWomenBgScrollView;
}



- (xsSelectDigitalRobotCollectionView *)meCollectionView{
    
    if(!_meCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<10; i++) {
//            xsSelectDigitalRobotCollectionViewCellModel *md = [xsSelectDigitalRobotCollectionViewCellModel new];
//            md.url = @"bossIPDefaultImage";
//            if(i!=0){
//                md.isAddType = NO;
//            }
//            else{
//                md.isAddType = YES;
//            }
//            [tempMarr addObject:md];
//        }
        
        // 视频比例 aspectRatio    视频比例 现在只有竖屏 传1就行   categoryId： 1是男的 2是女的 0是全部
        _meCollectionView = [[xsSelectDigitalRobotCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAspectRatio:1 withCategoryId:0];
        _meCollectionView.backgroundColor = UIColor.blackColor;
        _meCollectionView.delegateDigitalRobot = self;
    }
    return _meCollectionView;
}
- (xsSelectDigitalRobotCollectionView *)manCollectionView{
    
    if(!_manCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<10; i++) {
//            xsSelectDigitalRobotCollectionViewCellModel *md = [xsSelectDigitalRobotCollectionViewCellModel new];
//            md.url = @"bossIPDefaultImage";
//            if(i!=0){
//                md.isAddType = NO;
//            }
//            else{
//                md.isAddType = YES;
//            }
//            [tempMarr addObject:md];
//        }
        
        // 视频比例 aspectRatio    视频比例 现在只有竖屏 传1就行   categoryId： 1是男的 2是女的 0是全部
        _manCollectionView = [[xsSelectDigitalRobotCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAspectRatio:1 withCategoryId:1];
        _manCollectionView.delegateDigitalRobot = self;
        _manCollectionView.backgroundColor = UIColor.blackColor;
        
    }
    return _manCollectionView;
}
- (xsSelectDigitalRobotCollectionView *)womenCollectionView{
    
    if(!_womenCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<10; i++) {
//            xsSelectDigitalRobotCollectionViewCellModel *md = [xsSelectDigitalRobotCollectionViewCellModel new];
//            md.url = @"bossIPDefaultImage";
//            if(i!=0){
//                md.isAddType = NO;
//            }
//            else{
//                md.isAddType = YES;
//            }
//            [tempMarr addObject:md];
//        }
        
        // 视频比例 aspectRatio    视频比例 现在只有竖屏 传1就行   categoryId： 1是男的 2是女的 0是全部
        _womenCollectionView = [[xsSelectDigitalRobotCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAspectRatio:1 withCategoryId:2];
        _womenCollectionView.backgroundColor = UIColor.blackColor;
        _womenCollectionView.delegateDigitalRobot = self;
    }
    return _womenCollectionView;
}

- (UIView *)topView{
    
    if(!_topView){
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = UIColor.clearColor;
    }
    return _topView;
}



- (UIButton *)xButton{
    if (!_xButton) {
        _xButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xButton.backgroundColor = UIColor.clearColor;
        [_xButton addTarget:self action:@selector(xsXButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xButton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];
    }
    return _xButton;
}
- (void)xsXButtonAction:(UIButton *)btn{
    NSLog(@"xsXButtonAction\n\n");
    
    if([self.delegate respondsToSelector:@selector(xsSelectBossTemplateXButtonBeClick:)]){
        [self.delegate xsSelectBossTemplateXButtonBeClick:btn];
    }
}

- (UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = UIColor.purpleColor;
        [_searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setImage:[UIImage imageNamed:@"形状 22"] forState:UIControlStateNormal];
        _searchButton.hidden = YES;
    }
    return _searchButton;
}
- (void)searchButtonAction:(UIButton *)btn{
    NSLog(@"searchButtonAction\n\n");
    
    
}
- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = [UIFont systemFontOfSize:21];// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"选择老板IP模板";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    
}


- (void)updateConstraints{
    [super updateConstraints];
    //
    //    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    
    
}

- (void)xsChangeFrame{
    
    CGFloat margin10 = 10;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(50);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.xButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView.mas_right);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    
    
    self.buttonsBgScrollView.frame = CGRectMake(0, 50+10, self.bounds.size.width, 40);
    
    
    
    CGFloat myManWomenBgScrollViewY = CGRectGetMaxY(self.buttonsBgScrollView.frame)+margin10;
    CGFloat myManWomenBgScrollViewX = margin10;
    CGFloat myManWomenBgScrollViewW = self.width-myManWomenBgScrollViewX*2.0;
    CGFloat myManWomenBgScrollViewH = self.height - myManWomenBgScrollViewY-10;
    self.myManWomenBgScrollView.frame = CGRectMake(myManWomenBgScrollViewX, myManWomenBgScrollViewY, myManWomenBgScrollViewW, myManWomenBgScrollViewH);
    self.myManWomenBgScrollView.backgroundColor = UIColor.clearColor;
    self.myManWomenBgScrollView.contentSize = CGSizeMake(self.myManWomenBgScrollView.width*3.0, 0);
    
    self.meCollectionView.frame = CGRectMake(0, 0, self.myManWomenBgScrollView.width, self.myManWomenBgScrollView.height);
    self.manCollectionView.frame = CGRectMake(self.myManWomenBgScrollView.width, 0, self.myManWomenBgScrollView.width, self.myManWomenBgScrollView.height);
    self.womenCollectionView.frame = CGRectMake(self.myManWomenBgScrollView.width*2.0, 0, self.myManWomenBgScrollView.width, self.myManWomenBgScrollView.height);
    
    
    UIFont *btnFont = Font(16);
    
    
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < self.buttons.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *buttonTitleStr = self.buttons[i];
        
        CGFloat buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:40]+25.0;
        
        button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
        if(lastButton){
            //button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
            button.font = btnFont;
        }
        else{
            //button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
            button.font = FontBoldMT(16);
        }
        button.backgroundColor = UIColor.clearColor;//xrandomColor;
        [button setTitle:self.buttons[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.buttonsBgView addSubview:button];
        lastButton = button;
    }
    
    // 设置内容视图的尺寸
    self.buttonsBgView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 40);
    self.buttonsBgScrollView.contentSize = self.buttonsBgView.frame.size;
}

- (void)buttonClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
    // 在这里切换底部视图
    
    for (UIButton *tempButton in self.buttonsBgView.subviews) {
        if([tempButton  isKindOfClass:[UIButton class]]){
            if(tempButton.tag!=sender.tag){
                tempButton.font = Font(16);
            }
            else{
                tempButton.font = FontBoldMT(16);
            }
        }
    }
    XSWeakSelf;
    [UIView animateWithDuration:0.3 animations:^{
        [weakSelf.myManWomenBgScrollView setContentOffset:CGPointMake(weakSelf.myManWomenBgScrollView.width*selectedIndex, 0)];
    }];
}



#pragma mark  ScollViewDelegate
// 滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"scrollViewDidEndDecelerating");
    if(scrollView==self.myManWomenBgScrollView){
        int pageIndex = scrollView.contentOffset.x/self.myManWomenBgScrollView.frame.size.width;
        
        NSLog(@"scrollViewDidEndDecelerating--pageIndex==%d\n",pageIndex);
        for (UIButton *tempButton in self.buttonsBgView.subviews) {
            if([tempButton isKindOfClass:[UIButton class]])
            {
                if(pageIndex==tempButton.tag){
                    [self buttonClicked:tempButton];
                    break;
                }
            }
        }
    }
}

#pragma mark xsSelectDigitalRobotCollectionViewProtoCol
// 视频比例 aspectRatio    视频比例 现在只有竖屏 传1就行  categoryId： 1是男的 2是女的 0是我的
- (void)xsDigitalRobotCollectionCellBeClick:(xsSelectDigitalRobotCollectionViewCellModel *)model withAspectRatio:(NSInteger)aspectRatio withCategoryId:(NSInteger)categoryId{
    
    if(self.xsDigitalRobotCollectionCellCallbackBlock){
        self.xsDigitalRobotCollectionCellCallbackBlock(model, aspectRatio,categoryId);
    }
    
    //categoryId： 1是男的 2是女的 0是我的
    if(categoryId==1){//男的
    
        // 刷新女的数据
        // 刷新我的数据
        [_womenCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
        [_meCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    else if(categoryId==2){//女的
        // 刷新我的数据
        // 刷新男的数据
        [_meCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
        [_manCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    else if(categoryId==0){//我的
        // 刷新女的数据
        // 刷新男的数据
        [_womenCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
        [_manCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    
  
}

//categoryIdInt 1是男的 2是女的 0是我的
- (void)xsUpdateAllDataNotSelectWithCategoryId:(NSInteger)categoryIdInt{
    //categoryIdInt： 1是男的 2是女的 0是我的
    if(categoryIdInt==1){//男的
    
        [_manCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    else if(categoryIdInt==2){//女的
        [_womenCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    else if(categoryIdInt==0){//我的
        [_meCollectionView xsDigitalRobotCollectionUpdateCellModelNoSelect];
    }
    
}
@end
